package com.ruoyi.buy.service.impl;

import com.ruoyi.buy.domain.entity.PO.InvInoutBillItem;
import com.ruoyi.buy.domain.entity.PO.InvStockBill;
import com.ruoyi.buy.domain.entity.PO.InvStockBillItem;
import com.ruoyi.buy.domain.entity.inv0006.Inv0006InvDataDto;
import com.ruoyi.buy.domain.entity.inv0006.Inv0006UpdateByIdVO;
import com.ruoyi.buy.mapper.PO.InvStockBillItemMapper;
import com.ruoyi.buy.mapper.PO.InvStockBillMapper;
import com.ruoyi.buy.service.ICommonService;
import com.ruoyi.buy.service.IInvStockBillService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * 盘点单信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-02
 */
@Service
public class InvStockBillServiceImpl implements IInvStockBillService
{
    @Resource
    private InvStockBillMapper invStockBillMapper;

    @Resource
    private InvStockBillItemMapper invStockBillItemMapper;

    @Resource
    private ICommonService commonService;

    /**
     * 查询盘点单信息
     * 
     * @param id 盘点单信息主键
     * @return 盘点单信息
     */
    @Override
    public InvStockBill selectInvStockBillById(Long id)
    {
        return invStockBillMapper.selectInvStockBillById(id);
    }

    /**
     * 查询盘点单信息列表
     * 
     * @param invStockBill 盘点单信息
     * @return 盘点单信息
     */
    @Override
    public List<InvStockBill> selectInvStockBillList(InvStockBill invStockBill)
    {
        return invStockBillMapper.selectInvStockBillList(invStockBill);
    }

    /**
     * 新增盘点单信息
     * 
     * @param para 盘点单信息
     * @return 结果
     */
    @Override
    public int insertInvStockBill(Inv0006UpdateByIdVO para)
    {
        //先新增主表
        InvStockBill invStockBill = para.getInvStockBill();
        invStockBill.setCreateTime(DateUtils.getNowDate());
        invStockBill.setvBillNo(commonService.getNo("PD"));
        invStockBillMapper.insertInvStockBill(invStockBill);

        invStockBill = invStockBillMapper.selectInvStockBillList(invStockBill).get(0);
        Long nMainId = invStockBill.getId();

        List<InvStockBillItem> invStockBillItemList = para.getInvStockBillItemList();
        for (InvStockBillItem item :invStockBillItemList
             ) {
            item.setnMainId(nMainId);
            item.setCreateTime(DateUtils.getNowDate());
            invStockBillItemMapper.insertInvStockBillItem(item);
        }

        return 1;
    }

    /**
     * 修改盘点单信息
     * 
     * @param para 盘点单信息
     * @return 结果
     */
    @Override
    public int updateInvStockBill(Inv0006UpdateByIdVO para)
    {
        InvStockBill invStockBill = para.getInvStockBill();
        invStockBill.setCreateTime(DateUtils.getNowDate());
        invStockBillMapper.updateInvStockBill(invStockBill);

        List<InvStockBillItem> invStockBillItemList = para.getInvStockBillItemList();
        for (InvStockBillItem item :invStockBillItemList
        ) {
            if(item.getId()==null ||item.getId()==0 ){
                item.setnMainId(invStockBill.getId());
                item.setCreateTime(DateUtils.getNowDate());
                invStockBillItemMapper.insertInvStockBillItem(item);
            }else{
                item.setUpdateTime(DateUtils.getNowDate());
                invStockBillItemMapper.updateInvStockBillItem(item);
            }

        }
        return 1;
    }

    /**
     * 批量删除盘点单信息
     * 
     * @param ids 需要删除的盘点单信息主键
     * @return 结果
     */
    @Override
    public int deleteInvStockBillByIds(Long[] ids)
    {
        return invStockBillMapper.deleteInvStockBillByIds(ids);
    }

    /**
     * 删除盘点单信息信息
     * 
     * @param id 盘点单信息主键
     * @return 结果
     */
    @Override
    public int deleteInvStockBillById(Long id)
    {
        return invStockBillMapper.deleteInvStockBillById(id);
    }

    @Override
    public List<Inv0006InvDataDto> getInvDataList(InvStockBill para) {
        if(para.getvLocationId() != null && StringUtils.isEmpty(para.getvLocationId())){
            List<String> vLocationId = Arrays.asList(para.getvLocationId().split(","));
            para.setvLocationIds(vLocationId);
        }
        List<Inv0006InvDataDto> list= this.invStockBillMapper.getInvDataList(para);

        //再查这些天的变化量
        for (Inv0006InvDataDto item :list
             ) {
            item.setdBegin(para.getdBegin());

            item.setdEnd(para.getdEnd());
            BigDecimal nChangeQty = BigDecimal.ZERO;
            List<InvInoutBillItem> invInoutBillItems = this.invStockBillMapper.getInOutDataList(item);

            for (InvInoutBillItem inOutItem:invInoutBillItems
                 ) {
                if(inOutItem.getIotype().equals("I")){
                    nChangeQty = nChangeQty.add(inOutItem.getnQty());
                }
                else if(inOutItem.getIotype().equals("O")){
                    nChangeQty = nChangeQty.subtract(inOutItem.getnQty());
                }
            }
            item.setnChangeQty(nChangeQty);
            item.setnInvQty(item.getnQty().subtract(nChangeQty));
        }

        return list;
    }

    @Override
    public Inv0006UpdateByIdVO viewById(InvStockBill para) {
        Inv0006UpdateByIdVO result = new Inv0006UpdateByIdVO();
        para = this.invStockBillMapper.selectInvStockBillById(para.getId());

        InvStockBillItem invStockBillItem = new InvStockBillItem();
        invStockBillItem.setnMainId(para.getId());
        List<InvStockBillItem> invStockBillItemList = this.invStockBillItemMapper.selectInvStockBillItemList(invStockBillItem);
        result.setInvStockBill(para);
        result.setInvStockBillItemList(invStockBillItemList);
        return result;
    }

    @Override
    public int updateBillStatus(InvStockBill para) {
        InvStockBill invStockBill = this.invStockBillMapper.selectInvStockBillById(para.getId());
        invStockBill.setStatus(para.getStatus());
        this.invStockBillMapper.updateInvStockBill(invStockBill);
        return 1;
    }
}
